package org.design.environmentprotection.widget;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.design.environmentprotection.R;


/**
 * Created by jzh-创世头盔之父.
 * Date: 2019\10\28 0028* Time: 8:51
 * Emaile: 1186832904@qq.com
 * Introduction: jzh de code
 */
public class TopBar extends LinearLayout implements View.OnClickListener {

    private Context mContext;

    private View view;

    private LinearLayout ll_left, ll_right;

    private ImageView iv_left, iv_right;

    private TextView tv_center;

    private OnTopBarClickListener onTopBarClickListener;

    public void setOnTopBarClickListener(OnTopBarClickListener onTopBarClickListener) {
        this.onTopBarClickListener = onTopBarClickListener;
    }

    public interface OnTopBarClickListener {
        void onTopBarClick(boolean isLeft);
    }

    public TopBar(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {

        DisplayMetrics dm = getResources().getDisplayMetrics();
        final int screenHeigth = dm.heightPixels;
        final int screenWidth=dm.widthPixels;

        setOrientation(VERTICAL);
        setBackgroundColor(mContext.getResources().getColor(R.color.A1));
        LayoutParams layoutParams0 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        setLayoutParams(layoutParams0);


        LinearLayout linearLayout = new LinearLayout(mContext);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, getStatusBarHeight(mContext));
        linearLayout.setLayoutParams(layoutParams);
        addView(linearLayout);

        RelativeLayout relativeLayout = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, screenHeigth/18);
        relativeLayout.setLayoutParams(layoutParams1);

        RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(screenWidth/8, LayoutParams.MATCH_PARENT);
        layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        ll_left = new LinearLayout(mContext);
        ll_left.setLayoutParams(layoutParams2);
        ll_left.setGravity(Gravity.CENTER);
        iv_left = new ImageView(mContext);
        LayoutParams layoutParams4 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        iv_left.setLayoutParams(layoutParams4);
        ll_left.setOnClickListener(this);
        ll_left.addView(iv_left);
        relativeLayout.addView(ll_left);

        tv_center = new TextView(mContext);
        RelativeLayout.LayoutParams layoutParams5 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams5.addRule(RelativeLayout.CENTER_IN_PARENT);
        tv_center.setLayoutParams(layoutParams5);
        tv_center.setTextColor(mContext.getResources().getColor(R.color.A2));
        tv_center.setTextSize(18);
        relativeLayout.addView(tv_center);


        RelativeLayout.LayoutParams layoutParams6 = new RelativeLayout.LayoutParams(screenWidth/8, LayoutParams.MATCH_PARENT);
        layoutParams6.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        ll_right = new LinearLayout(mContext);
        ll_right.setLayoutParams(layoutParams6);
        ll_right.setGravity(Gravity.CENTER);
        iv_right = new ImageView(mContext);
        LayoutParams layoutParams7 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        iv_right.setLayoutParams(layoutParams7);
        ll_right.setOnClickListener(this);
        ll_right.addView(iv_right);
        ll_right.setOnClickListener(this);
        relativeLayout.addView(ll_right);

        addView(relativeLayout);

        setImageResource(true, R.drawable.back);
        setImageResource(false, R.drawable.back);
        setText("资讯");
    }

    public void setVisible(boolean isLeft, boolean isShow) {
        (isLeft ? ll_left : ll_right).setVisibility(isShow ? VISIBLE : GONE);
    }

    public void setImageResource(boolean isLeft, int resource) {
        (isLeft ? iv_left : iv_right).setImageResource(resource);
    }

    public void setText(String text) {
        tv_center.setText(text);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ll_left:
                if (onTopBarClickListener != null) {
                    onTopBarClickListener.onTopBarClick(true);
                }
                break;
            case R.id.ll_right:
                if (onTopBarClickListener != null) {
                    onTopBarClickListener.onTopBarClick(false);
                }
                break;
        }
    }

    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }
}
